package com.xinchen.gufeiwulian.ui.activity

import android.os.Bundle
import android.view.KeyEvent
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.maple.base.base.BaseActivity
import com.maple.utils.manager.MyFragmentStateAdapter
import com.maple.utils.util.LogUtils
import com.maple.utils.util.UIUtils
import com.xinchen.gufeiwulian.R
import com.xinchen.gufeiwulian.ui.fragment.MainFragment
import com.xinchen.gufeiwulian.ui.fragment.MessageFragment
import com.xinchen.gufeiwulian.ui.fragment.MineFragment
import com.xinchen.gufeiwulian.vm.HomeViewModel

class HomeActivity : BaseActivity() {

    private var pager: ViewPager2? = null
    private var bnav: BottomNavigationView? = null

    private var lastBackPressedMillis: Long = 0L
    override fun getLayoutId(): Int = R.layout.activity_home

    private val viewModel by viewModels<HomeViewModel>()

    private val list by lazy {
        listOf(
            MainFragment.getInstance(viewModel),
            MessageFragment.getInstance(viewModel),
            MineFragment.getInstance(viewModel)
        )
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        pager = findViewById(R.id.pager)
        bnav = findViewById(R.id.bnav)
    }

    override fun initData(savedInstanceState: Bundle?) {
        viewModel.defUI.showDialog.observe(this, Observer {
            showLoading()
        })

        viewModel.defUI.dismissDialog.observe(this, Observer {
            dismissLoading()
        })

        viewModel.defUI.toastEvent.observe(this, Observer {
            showToast(it)
        })
        pager?.let {
            it.offscreenPageLimit = list.size
            it.isUserInputEnabled = false
            val adapter: MyFragmentStateAdapter = MyFragmentStateAdapter(this, list)
            it.orientation = ViewPager2.ORIENTATION_HORIZONTAL
            it.adapter = adapter
            it.currentItem = 0
            it.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    bnav?.menu?.getItem(position)?.isChecked = true
                }
            })
        }

        bnav?.setOnItemSelectedListener { item ->
            LogUtils.logGGQ("===item===>>${item.itemId}")
            when (item.itemId) {
                R.id.item_nav_main -> {
                    pager?.currentItem = 0
                }

                R.id.item_nav_message -> {
                    pager?.currentItem = 1
                }

                R.id.item_nav_mine -> {
                    pager?.currentItem = 2
                }
            }
            false
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (lastBackPressedMillis + 2000L > System.currentTimeMillis()) {
            moveTaskToBack(true)
        } else {
            lastBackPressedMillis = System.currentTimeMillis()
            showToast(UIUtils.getString(com.maple.res.R.string.exit_app))
        }
        return super.onKeyDown(keyCode, event)
    }

}